<template>
  <el-dialog
    :title="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    class="JADP-dialog JADP-dialog_center"
    lock-scroll
    width="800px"
    append-to-body
  >
    <el-row :gutter="15" class="">
      <el-form
        ref="elForm"
        :model="dataForm"
        :rules="rules"
        size="small"
        label-width="100px"
        label-position="right"
        :disabled="!!isDetail"
      >
        <el-col :span="24">
          <el-form-item label="分类" prop="classification">
            <JADP-TreeSelect
              v-model="dataForm.classification"
              :options="treeData"
              placeholder="选择分类"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="试卷类型" prop="testpapertype">
            <el-radio-group v-model="dataForm.testpapertype" size="small">
              <el-radio
                v-for="(item, index) in testpapertypeOptions"
                :key="index"
                :label="item.id"
                :disabled="item.disabled"
              >
                {{ item.fullName }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="出题方式" prop="questionmethod">
            <el-radio-group v-model="dataForm.questionmethod" size="small">
              <el-radio
                v-for="(item, index) in questionmethodOptions"
                :key="index"
                :label="item.id"
                :disabled="item.disabled"
              >
                {{ item.fullName }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="试卷名称" prop="testpapername">
            <el-input
              v-model="dataForm.testpapername"
              placeholder="请输入"
              clearable
              :style="{ width: '100%' }"
            >
            </el-input>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="描述" prop="description">
            <el-input
              v-model="dataForm.description"
              placeholder="请输入"
              clearable
              :style="{ width: '100%' }"
            >
            </el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import request from "@/utils/request";
import { getDictionaryDataSelector } from "@/api/systemData/dictionary";
import ExportBox from "./ExportBox";
export default {
  components: { ExportBox },
  props: [],
  data() {
    return {
      loading: false,
      visible: false,
      isDetail: false,
      activeName: "",
      dataForm: {
        classification: "",
        testpapertype: "2bf40acb3b0a4eb494f9a30cb9b31afe",
        questionmethod: "b876a8387d6e4b588573bf74a557ed1b",
        testpapername: undefined,
        creatoruserid: "",
        creatortime: "",
        lastmodifyuserid: "",
        lastmodifytime: "",
        releasestatus: "699d34eb56534be8ac1b7fb0d3fd1dbf",
        examinationstatus: undefined,
        examinationresults: undefined,
        description: "",
        organizeId:""
      },
      rules: {
        testpapername: [
          {
            required: true,
            message: "请输入",
            trigger: "blur"
          }
        ]
      },

      testpapertypeOptions: [],
      questionmethodOptions: [],
      releasestatusOptions: [],
      treeData: []
    };
  },
  computed: {},
  watch: {},
  created() {
    this.gettestpapertypeOptions();
    this.getquestionmethodOptions();
    this.getreleasestatusOptions();
    this.getTreeData();
  },
  mounted() {},
  methods: {
    getTreeData() {
      this.request({
        url: "/api/xy_ks_paper_type/Xy_ks_paper_type/getTreeList",
        method: "get"
      }).then(res => {
        let topItem = {
          fullName: "试卷分类",
          hasChildren: true,
          id: "-1",
          // icon: "icon-ym icon-ym-tree-organization3",
          children: res.data
        };
        this.treeData = [topItem];
      });
    },
    gettestpapertypeOptions() {
      getDictionaryDataSelector("44a600feb4594270bf5c7c3eeb9ac6b5").then(
        res => {
          this.testpapertypeOptions = res.data.list;
        }
      );
    },
    getquestionmethodOptions() {
      getDictionaryDataSelector("3c8e608a8cf6486ba13a980c2b3deed5").then(
        res => {
          this.questionmethodOptions = res.data.list;
        }
      );
    },
    getreleasestatusOptions() {
      getDictionaryDataSelector("e90c8c4b258f48fd9830c35d65773d92").then(
        res => {
          this.releasestatusOptions = res.data.list;
        }
      );
    },
    init(id, isDetail) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.isDetail = isDetail || false;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          request({
            url:
              "/api/testPaperManage/Xy_ks_examination_paper/" +
              this.dataForm.id,
            method: "get"
          }).then(res => {
            this.dataForm = res.data;
            this.loading = false;
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["elForm"].validate(valid => {
        if (valid) {
          this.dataForm.organizeId= this.$store.getters.organizeId;
          if (!this.dataForm.id) {
            request({
              url: `/api/testPaperManage/Xy_ks_examination_paper`,
              method: "post",
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: "success",
                duration: 1000,
                onClose: () => {
                  (this.visible = false), this.$emit("refresh", true);
                }
              });
            });
          } else {
            request({
              url:
                "/api/testPaperManage/Xy_ks_examination_paper/" +
                this.dataForm.id,
              method: "PUT",
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refresh", true);
                }
              });
            });
          }
        }
      });
    }
  }
};
</script>
